package java5.enumeration.part37;

import java.util.*;

/**
 * EnumMap 用法
 */
public class Plant {
    enum LifeCycle { ANNUAL, PERENNIAL, BIENNIAL }
    final String name;
    final LifeCycle lifeCycle;

    Plant(String name, LifeCycle lifeCycle) {
        this.name = name;
        this.lifeCycle = lifeCycle;
    }

    @Override
    public String toString() {
        return name;
    }

    /**
     * EnumMap 的使用
     * 将花园的 根据生命周期分类
     */
    public static Map<LifeCycle, Set<Plant>> groupPlant(Collection<Plant> garden) {
        Map<Plant.LifeCycle, Set<Plant>> plantsByLifeCycle = new EnumMap<>(Plant.LifeCycle.class);
        for (Plant.LifeCycle le : Plant.LifeCycle.values()) {
            plantsByLifeCycle.put(le, new HashSet<>());
        }
        for (Plant p : garden) {
            plantsByLifeCycle.get(p.lifeCycle).add(p);
        }

        //java8 版本
        //garden.stream().collect(Collectors.groupingBy(p->p.lifeCycle,()->new EnumMap<>(Plant.LifeCycle.class),Collectors.toSet()));
        return plantsByLifeCycle;
    }

}